package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.web.page.PagePlus;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.logi.sys.bo.LogiWaybillEditExtBo;
import com.piggy.logi.sys.domain.LogiWaybill;
import com.piggy.logi.sys.enums.WaybillKefuLogTypeEnum;
import com.piggy.logi.sys.service.ILogiWaybillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.piggy.logi.sys.bo.LogiWaybillKefuLogQueryBo;
import com.piggy.logi.sys.bo.LogiWaybillKefuLogEditBo;
import com.piggy.logi.sys.domain.LogiWaybillKefuLog;
import com.piggy.logi.sys.mapper.LogiWaybillKefuLogMapper;
import com.piggy.logi.sys.vo.LogiWaybillKefuLogVo;
import com.piggy.logi.sys.service.ILogiWaybillKefuLogService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 运单客服日志信息Service业务层处理
 *
 * @author piggy
 * @date 2023-10-25
 */
@Slf4j
@Service
public class LogiWaybillKefuLogServiceImpl extends ServiceImpl<LogiWaybillKefuLogMapper, LogiWaybillKefuLog> implements ILogiWaybillKefuLogService {

    @Resource
    private ILogiWaybillService waybillService;

    @Override
    public LogiWaybillKefuLogVo queryById(Long id) {
        return getVoById(id, LogiWaybillKefuLogVo.class);
    }

    @Override
    public TableDataInfo<LogiWaybillKefuLogVo> queryPageList(LogiWaybillKefuLogQueryBo bo) {
        PagePlus<LogiWaybillKefuLog, LogiWaybillKefuLogVo> result = pageVo(PageUtils.buildPagePlus(bo, true), buildQueryWrapper(bo), LogiWaybillKefuLogVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<LogiWaybillKefuLogVo> queryList(LogiWaybillKefuLogQueryBo bo) {
        return listVo(buildQueryWrapper(bo), LogiWaybillKefuLogVo.class);
    }

    private LambdaQueryWrapper<LogiWaybillKefuLog> buildQueryWrapper(LogiWaybillKefuLogQueryBo bo) {
        LambdaQueryWrapper<LogiWaybillKefuLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWaybillId() != null, LogiWaybillKefuLog::getWaybillId, bo.getWaybillId());
        lqw.eq(bo.getCargoControlStatus() != null, LogiWaybillKefuLog::getCargoControlStatus, bo.getCargoControlStatus());
        lqw.eq(bo.getEnableStatus() != null, LogiWaybillKefuLog::getEnableStatus, bo.getEnableStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getLogType()), LogiWaybillKefuLog::getLogType, bo.getLogType());
        lqw.eq(StrUtil.isNotBlank(bo.getLrPerson()), LogiWaybillKefuLog::getLrPerson, bo.getLrPerson());
        lqw.eq(StrUtil.isNotBlank(bo.getLrCardNo()), LogiWaybillKefuLog::getLrCardNo, bo.getLrCardNo());
        lqw.eq(StrUtil.isNotBlank(bo.getLrContactPerson()), LogiWaybillKefuLog::getLrContactPerson, bo.getLrContactPerson());
        lqw.orderByDesc(LogiWaybillKefuLog::getCreateTime);
        return lqw;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogiWaybillKefuLogVo insertByAddBo(LogiWaybillKefuLogEditBo bo) {
        LogiWaybillKefuLog add = BeanUtil.toBean(bo, LogiWaybillKefuLog.class);
        save(add);
        if (bo.getLogType().equals(WaybillKefuLogTypeEnum.LOG_CAGO_CONTROL.getCode())) {
//            waybillService.update(Wrappers.<LogiWaybill>lambdaUpdate().set(LogiWaybill::getCargoControlStatus, bo.getCargoControlStatus()).eq(LogiWaybill::getWaybillId, bo.getWaybillId()));
            waybillService.updateByEditBo((LogiWaybillEditExtBo) new LogiWaybillEditExtBo().setWaybillId(bo.getWaybillId()).setCargoControlStatus(bo.getCargoControlStatus()));
        }
        if (bo.getLogType().equals(WaybillKefuLogTypeEnum.LOG_ENABLE.getCode())) {
            waybillService.updateByEditBo((LogiWaybillEditExtBo) new LogiWaybillEditExtBo().setWaybillId(bo.getWaybillId()).setEnableStatus(bo.getEnableStatus()));
        }

        return queryById(add.getId());
    }

    @Override
    @Transactional
    public LogiWaybillKefuLogVo updateByEditBo(LogiWaybillKefuLogEditBo bo) {
        LogiWaybillKefuLog update = BeanUtil.toBean(bo, LogiWaybillKefuLog.class);
        updateById(update);
        return queryById(update.getId());
    }

    @Override
    @Transactional
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public LogiWaybillKefuLogVo getLrLog(Long billId, Integer enableStatus) {
        LambdaQueryWrapper<LogiWaybillKefuLog> wrapper = buildQueryWrapper(new LogiWaybillKefuLogQueryBo().setWaybillId(billId).setLogType(WaybillKefuLogTypeEnum.LOG_ENABLE.getCode()).setEnableStatus(enableStatus));
        wrapper.orderByDesc(LogiWaybillKefuLog::getId);
        wrapper.last("limit 1");
        return this.getVoOne(wrapper, LogiWaybillKefuLogVo.class);
    }

    @Override
    public List<LogiWaybillKefuLogVo> getLrLogList(List<Long> billIds, Integer enableStatus) {
        LambdaQueryWrapper<LogiWaybillKefuLog> wrapper = buildQueryWrapper(new LogiWaybillKefuLogQueryBo().setLogType(WaybillKefuLogTypeEnum.LOG_ENABLE.getCode()).setEnableStatus(enableStatus));
        wrapper.in(LogiWaybillKefuLog::getWaybillId, billIds);
        wrapper.orderByDesc(LogiWaybillKefuLog::getId);
        return this.listVo(wrapper, LogiWaybillKefuLogVo.class);
    }
}
